﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tsanie.AzerothReader.Util;
using Tsanie.AzerothReader.Network;
using System.IO;
using System.Net;

namespace Tsanie.AzerothReader {
    public partial class MainReader : Form {
        string email = "";
        string password = "";

        public MainReader() {
            InitializeComponent();
            this.Text = Config.c_AppTitle;

            string url = Config.c_178Host + "/q_account.php";
            NetBase.BeginConnect(url, (request) => {
                request.Method = "POST";
                request.Referer = Config.c_178Host + "/?p=trylogin&to=" + Config.c_NgaHost;
                request.Headers["Cache-Control"] = "max-age=0";
                request.Headers["Origin"] = Config.c_178Host;
                request.ContentType = "application/x-www-form-urlencoded";
                request.AllowAutoRedirect = false;
                byte[] data = Encoding.UTF8.GetBytes("_act=login&to=http%3A%2F%2Fbbs.ngacn.cc&type=email&email=" + HttpUtil.UrlEncode(email)
                    + "&password=" + password + "&noexpires=31536000");
                request.ContentLength = data.Length;
                using (Stream stream = request.GetRequestStream()) {
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();
                }
            }, (state) => {
                string cookie = state.Response.Headers["Set-Cookie"];
                if (cookie == null)
                    return;
                string cid = null, uid = null;
                foreach (string par in cookie.Split(new char[] { ',', ';' })) {
                    if (par.StartsWith("_sid=")) {
                        cid = par.Substring(5);
                    } else if (par.StartsWith("_178c")) {
                        uid = par.Substring(6, par.IndexOf("%23") - 6);
                    }
                }
                NetBase.BeginConnect(Config.c_NgaHost + "/nuke.php?func=login&uid=" + uid + "&cid=" + cid + "&expires=31536000", (request) => {
                    request.Referer = state.Response.ResponseUri.AbsolutePath;
                }, new Action<RequestState>(NgaResponse), null);
            }, null);
        }

        private void NgaResponse(RequestState state) {
            string ck = state.Response.Headers["Set-Cookie"];
            if (ck == null)
                return;
            string cookie = "";
            foreach (string par in ck.Split(new char[] { ',', ';' })) {
                if (par.StartsWith("nga")) {
                    cookie += par + "; ";
                }
            }
            if (cookie.Length > 0)
                cookie = cookie.Substring(0, cookie.Length - 2);
            NetBase.BeginConnect(Config.c_NgaHost, (request) => { request.Headers["Cookie"] = cookie; }, (st) => {
                using (StreamReader reader = new StreamReader(st.StreamResponse, Encoding.GetEncoding("gbk"))) {
                    string str = reader.ReadToEnd();

                    reader.Close();
                }
            }, null);
        }
    }
}
